"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 统一数据库管理工具
Case Name   : 主节点宕机，验证是否可以自动备升主
Create At   : 2022-07-07
Owner       : opentestcase007
Description :
    1.停止主节点数据库
    2.查看集群状态
    3.停止主节点数据库
    4.恢复原集群状态
Expect      :
    1.停止主节点数据库成功
    2.查看集群状态成功，主机挂掉，备机自动升主
    3.停止主节点数据库成功
    4.恢复原集群状态成功
History     : 
"""

import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

Pri_U_SH = CommonSH('PrimaryDbUser')
Node_Num = Pri_U_SH.get_node_num()


@unittest.skipIf(3 > Node_Num, '不满足一主两备环境跳过')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.dbuser_node = Node('dbuser')
        self.log.info(f"-----{os.path.basename(__file__)} start-----")

    def test_server_tools1(self):
        text = '--step1:停止主节点数据库 expect:设置成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl stop -n 1 -D {macro.DB_INSTANCE_PATH};'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: stop instance successfully', cmctl_cmd)
        time.sleep(30)

        text = '--step2:查看集群状态 expect:查看成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl query -v -C -i;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('6001     P Down    Manually stopped', cmctl_cmd)
        self.assertIn('S Primary Normal', cmctl_cmd)

        text = '--step3:拉起原主节点数据库 expect:拉起成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl start -n 1 -D {macro.DB_INSTANCE_PATH};'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: start instance successfully', cmctl_cmd)
        time.sleep(10)

        text = '--step4:恢复原集群状态 expect:恢复成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl switchover -a;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover successfully', cmctl_cmd)

    def tearDown(self):
        self.log.info('--无需清理环境--')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
